include(FetchContent)

################################################################
# test_resource.git :  depends and resource
FetchContent_Declare(resource
        GIT_REPOSITORY https://gitee.com/histreamer/test_resource.git
        GIT_TAG master
        )

SET(RESOURCE_SRC ${CMAKE_BINARY_DIR}/_deps/resource-src)
if(NOT EXISTS ${RESOURCE_SRC})
    message("-- Download resource and copy depends. Resource directory : " ${RESOURCE_SRC})
    FetchContent_Populate(resource)
    file(COPY ${resource_SOURCE_DIR}/depends/3rdparty DESTINATION ${TOP_DIR})
else()
    message("-- Resource directory exists : " ${RESOURCE_SRC})
endif()
add_definitions(-DRESOURCE_DIR="${RESOURCE_SRC}/media")

if (AVS3DA_ENABLE)
    add_definitions(-DAVS3DA_CODEC_DIR="${HISTREAMER_PLUGINS_DIR}/codec/avs3_audio_decoder/avs3_codec/avs3Decoder/")
    if (NOT EXISTS ${RESOURCE_SRC}/media/AVS3DA AND EXISTS ${HISTREAMER_PLUGINS_DIR}/media/AVS3DA)
        file(COPY ${HISTREAMER_PLUGINS_DIR}/media/AVS3DA DESTINATION ${RESOURCE_SRC}/media)
    endif()
endif()

################################################################
# multimedia_histreamer.git :  Create symbolic link of histreamer-res
FetchContent_Declare(histreamer
        GIT_REPOSITORY https://gitee.com/openharmony/multimedia_histreamer.git
        GIT_TAG master
        )

macro(cmake_path_to_win_path InputPath ResultPath)
    string(REPLACE "/" "\\" ${ResultPath} "${InputPath}")
endmacro()

set(HISTREAMER_SRC_PATH ${CMAKE_BINARY_DIR}/_deps/histreamer-src)
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/histreamer)
    message("-- Download histreamer ...")
    FetchContent_Populate(histreamer)
    if (CMAKE_HOST_UNIX)
        execute_process(COMMAND ln -s ${HISTREAMER_SRC_PATH} ${CMAKE_SOURCE_DIR}/histreamer OUTPUT_VARIABLE output_result)
    else()
        cmake_path_to_win_path(${HISTREAMER_SRC_PATH} WIN_HISTREAMER_SRC_PATH)
        cmake_path_to_win_path(${CMAKE_SOURCE_DIR}/histreamer WIN_HISTREAMER_LINK_PATH)
        execute_process(COMMAND cmd.exe /c mklink /j ${WIN_HISTREAMER_LINK_PATH} ${WIN_HISTREAMER_SRC_PATH}
                OUTPUT_VARIABLE output_result ERROR_VARIABLE error_result ENCODING UTF-8 )
    endif()
    message("-- Create histreamer symbolic link :")
    message("     Info: " ${output_result})
else()
    message("-- histreamer directory exists.")
endif()
add_subdirectory(histreamer)
